<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="../ct-failure-stream.html">

<script>
(function () {

var assert = chai.assert;

describe('ct-failure-stream', function() {

  var stream;

  beforeEach(function(done) {
    stream = document.createElement('ct-failure-stream');
    setTimeout(done);
  });

  describe('default ui', function() {
    it('should show failure groups', function(done) {
      // FIXME: We should use some more interesting test data, but
      // ct-failure-stream will give these values to ct-commit-data,
      // which will hit the network unless we figure out how to mock
      // out ct-commit-data in a better way.
      stream.groups = [new CTFailureGroup('', new CTStepFailureGroupData([])),
          new CTFailureGroup('', new CTStepFailureGroupData([]))];
      stream.category = 'default';
      setTimeout(function() {
        var cards = stream.shadowRoot.querySelectorAll('ct-step-failure-card');
        assert.equal(cards.length, 2);
        done();
      });
    });
  });

  describe('category', function() {

    it('should only show failure groups for the specified category', function(done) {
      var failures = [new CTStepFailure('step', 'reason', [{key: 'a', annotation: {snoozeTime: Date.now() + 1000 * 1000}}])];
      var snoozed = new CTFailureGroup('', new CTStepFailureGroupData(failures));
      stream.groups = [new CTFailureGroup('', new CTStepFailureGroupData([])), snoozed];
      stream.category = 'snoozed';
      setTimeout(function() {
        var cards = stream.shadowRoot.querySelectorAll('ct-step-failure-card');
        assert.equal(cards.length, 1);
        assert.equal(cards[0].group, snoozed.data);
        done();
      });
    });

    it('should select the correct card type', function(done) {
      var failures = [new CTStepFailure('step', 'reason', [{key: 'a', annotation: {}}])];
      var failureGroup = new CTFailureGroup('', new CTStepFailureGroupData(failures));
      stream.groups = [new CTFailureGroup('', new CTStepFailureGroupData([])), failureGroup];
      stream.category = 'default';
      setTimeout(function() {
        assert(stream.shadowRoot.querySelector('ct-step-failure-card') != null,
            'missing sheriff card');
        stream.groups = [new CTFailureGroup('', new CTTrooperFailureGroupData(
            'details', 'url', {percent_over_median_slo: '6%',
            percent_over_max_slo: '7%'}, 'cq_latency', ''))];
        setTimeout(function() {
          assert(stream.shadowRoot.querySelector('ct-trooper-card') == null,
              'do not display cq-latency card');
          stream.groups[0].data.type = 'tree_status';
          setTimeout(function() {
            assert(stream.shadowRoot.querySelector('ct-trooper-card') ==
                null, 'do not display tree-status card');
            stream.groups[0].data.type = 'cycle_time';
            setTimeout(function() {
              assert(stream.shadowRoot.querySelector('ct-trooper-card') ==
                  null, 'do not display cycle-time card');
              done();
            });
          });
        });
      });
    });

    it('adding a bug number should show bugs link', function(done) {
      var cl = new CTCommitListMock();
      var group = new CTFailureGroup('blink', new CTStepFailureGroupData([
          new CTStepFailure('autobot', 'unknown', {someBuilder: {key: 'a', failingBuildCount: 2}},
              {'blink':158547}, {'blink':158544}),
        ], cl));
      group.setBug(123);

      stream.groups = [group];
      stream.category = 'default';

      setTimeout(function() {
        var links = stream.shadowRoot.querySelectorAll('.bugs a');
        assert.lengthOf(links, 1);
        assert.match(links[0].href, /crbug.com\/123/);
        done();
      });
    });

    it('should not show bugs link without a bug number', function(done) {
      var cl = new CTCommitListMock();
      var group = new CTFailureGroup('blink', new CTStepFailureGroupData([
        new CTStepFailure('autobot', 'unknown', {someBuilder: {key: 'a'}}, {'blink':158547},
                      {'blink':158544})], cl));

      stream.groups = [group];
      stream.category = 'default';

      setTimeout(function() {
        var links = stream.shadowRoot.querySelectorAll('.bugs');
        assert.lengthOf(links, 0);
        done();
      });
    });
  });
});

})();
</script>
